package com.egao.orderB.biz.impl;

import com.egao.common.core.damon.constant.UserConstant;
import com.egao.common.core.damon.results.BaseDao;
import com.egao.common.core.damon.results.PageResult2;

import com.egao.common.core.damon.utils.Utils;
import com.egao.common.system.entity.User;
import com.egao.orderB.biz.PutGoodsBiz;
import com.egao.orderB.entity.PutGoods;
import com.egao.common.core.web.JsonResult;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.Map;
import java.util.List;

/**
 *
 * 数据层实现类
 * @author : Doman
 * @date : 2021-12-23 15:04:33
 * @version ：1.0
 */
@Repository
public class PutGoodsBizImpl extends BaseDao implements PutGoodsBiz {

	@Override
	public JsonResult list(PageResult2<T> page, PutGoods putGoods, User loginUser) throws Exception {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<PutGoods> rm = new BeanPropertyRowMapper<PutGoods>(PutGoods.class);
		StringBuffer sql = new StringBuffer("select p.id,p.put_in_storage_id,p.shipping_mark");
    sql.append(",p.customer_id,p.customer");
    sql.append(",p.sku,p.name,p.stock_num,p.remark");
		sql.append(",p.num,p.pack,p.weight,p.total_weight");
		sql.append(",p.length,p.width,p.high,p.volume");
		sql.append(",p.total_volume,p.pallet_num,p.order_num,p.create_time");
		sql.append(",p.create_user,p.update_time,p.update_user");
    sql.append(",suser.nick_name createName ");
    sql.append(" from chf_put_goods p ");
    sql.append(" inner join sys_user suser on suser.user_id=p.create_user ");
    sql.append(" where p.deleted=0 ");
		sql.append(" and p.put_in_storage_id=:put_in_storage_id ");
		map.put("put_in_storage_id", putGoods.getPutInStorageId()+"");
		if(loginUser.getUserId() != UserConstant.ADMIN_ID){
      sql.append(" and p.organization_id=:tableOrganizationId ");
      map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
    }
    if(Utils.isNotEmpty(putGoods)){
//			if(Utils.isNotEmpty(putGoods.getName())){
//				sql.append(" and instr(p.name,:name)>0 ");
//				map.put("name", putGoods.getName());
//			}
      if(Utils.isNotEmpty(putGoods.getSort())&&Utils.isNotEmpty(putGoods.getOrder())){
        sql.append(" order by p."+putGoods.getSort()+" "+putGoods.getOrder());
      } else {
        sql.append(" order by p.create_time desc");
      }
		}
		return jsonResult(getPageQueryList(page, sql.toString(), map, rm));
	}

	@Override
	public List<PutGoods> list(PutGoods putGoods, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<PutGoods> rm = new BeanPropertyRowMapper<PutGoods>(PutGoods.class);
    StringBuffer sql = new StringBuffer("select p.id,p.put_in_storage_id,p.shipping_mark");
		sql.append(",p.customer_id,p.customer");
    sql.append(",p.sku,p.name,p.stock_num,p.remark");
		sql.append(",p.num,p.pack,p.weight,p.total_weight");
		sql.append(",p.length,p.width,p.high,p.volume");
		sql.append(",p.total_volume,p.pallet_num,p.order_num,p.create_time");
		sql.append(",p.create_user,p.update_time,p.update_user");
    sql.append(",suser.nick_name createName ");
    sql.append(" from chf_put_goods p ");
    sql.append(" inner join sys_user suser on suser.user_id=p.create_user ");
    sql.append(" where p.deleted=0 ");
		sql.append(" and p.put_in_storage_id=:put_in_storage_id ");
		map.put("put_in_storage_id", putGoods.getPutInStorageId()+"");
		if(loginUser.getUserId() != UserConstant.ADMIN_ID){
      sql.append(" and p.organization_id=:tableOrganizationId ");
      map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
    }
    if(Utils.isNotEmpty(putGoods)){
      if(Utils.isNotEmpty(putGoods.getSort())&&Utils.isNotEmpty(putGoods.getOrder())){
        sql.append(" order by p."+putGoods.getSort()+" "+putGoods.getOrder());
      } else {
        sql.append(" order by p.create_time desc");
      }
    }
		getMapParameter(sql, map);
		return jdbcTemplate.query(sql.toString(),map, rm);
	}

	@Override
	public PutGoods getById(PutGoods putGoods) {
		RowMapper<PutGoods> rm = new BeanPropertyRowMapper<PutGoods>(PutGoods.class);
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql = new StringBuffer("select p.id,p.put_in_storage_id,p.shipping_mark");
		sql.append(",p.customer_id,p.customer");
    sql.append(",p.sku,p.name,p.stock_num,p.remark");
		sql.append(",p.num,p.pack,p.weight,p.total_weight");
		sql.append(",p.length,p.width,p.high,p.volume");
		sql.append(",p.total_volume,p.pallet_num,p.order_num,p.create_time");
		sql.append(",p.create_user,p.update_time,p.update_user");
    sql.append(",suser.nick_name createName ");
    sql.append(" from chf_put_goods p ");
    sql.append(" inner join sys_user suser on suser.user_id=p.create_user ");
    sql.append(" where p.deleted=0 and p.id=:id ");
		map.put("id", putGoods.getId());
		getMapParameter(sql, map);
		return jdbcTemplate.queryForObject(sql.toString(), map, rm);
	}

	@Override
	public boolean save(PutGoods putGoods, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("insert into chf_put_goods (");
    sql.append("customer_id,customer");
    sql.append(",put_in_storage_id,shipping_mark,sku,name");
		sql.append(",stock_num,remark,num,pack");
		sql.append(",weight,total_weight,length,width");
		sql.append(",high,volume,total_volume,pallet_num");
		sql.append(",order_num,create_user,create_time) values ( ");
		sql.append(":customer_id,:customer");
    sql.append(",:put_in_storage_id,:shipping_mark,:sku,:name");
		sql.append(",:stock_num,:remark,:num,:pack");
		sql.append(",:weight,:total_weight,:length,:width");
		sql.append(",:high,:volume,:total_volume,:pallet_num");
		sql.append(",:order_num,:create_user,now())");
    map.put("put_in_storage_id", putGoods.getPutInStorageId()==null?null:putGoods.getPutInStorageId()+"");
    map.put("shipping_mark", putGoods.getShippingMark());
    map.put("customer_id", putGoods.getCustomerId()+"");
    map.put("customer", putGoods.getCustomer());
    map.put("sku", putGoods.getSku());
    map.put("name", putGoods.getName());
    map.put("stock_num", putGoods.getStockNum());
    map.put("remark", putGoods.getRemark());
    map.put("num", putGoods.getNum()+"");
    map.put("pack", putGoods.getPack()+"");
    map.put("weight", putGoods.getWeight()+"");
    map.put("total_weight", putGoods.getTotalWeight()+"");
    map.put("length", putGoods.getLength()+"");
    map.put("width", putGoods.getWidth()+"");
    map.put("high", putGoods.getHigh()+"");
    map.put("volume", putGoods.getVolume()+"");
    map.put("total_volume", putGoods.getTotalVolume()+"");
    map.put("pallet_num", putGoods.getPalletNum());
    map.put("order_num", putGoods.getOrderNum()==null?null:putGoods.getOrderNum()+"");
		map.put("create_user", loginUser.getUserId() + "");
		getMapParameter(sql, map);
		int count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean update(PutGoods putGoods, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("update chf_put_goods set ");
    sql.append("shipping_mark=:shipping_mark,sku=:sku ");
		sql.append(",name=:name,stock_num=:stock_num,remark=:remark ");
		sql.append(",num=:num,pack=:pack,weight=:weight ");
		sql.append(",total_weight=:total_weight,length=:length,width=:width ");
		sql.append(",high=:high,volume=:volume,total_volume=:total_volume ");
		sql.append(",pallet_num=:pallet_num,order_num=:order_num,update_time=now() ");
		sql.append(",update_user=:update_user where id=:id ");
    map.put("shipping_mark", putGoods.getShippingMark());
    map.put("sku", putGoods.getSku());
    map.put("name", putGoods.getName());
    map.put("stock_num", putGoods.getStockNum());
    map.put("remark", putGoods.getRemark());
    map.put("num", putGoods.getNum()+"");
    map.put("pack", putGoods.getPack());
    map.put("weight", putGoods.getWeight()+"");
    map.put("total_weight", putGoods.getTotalWeight()+"");
    map.put("length", putGoods.getLength()+"");
    map.put("width", putGoods.getWidth()+"");
    map.put("high", putGoods.getHigh()+"");
    map.put("volume", putGoods.getVolume()+"");
    map.put("total_volume", putGoods.getTotalVolume()+"");
    map.put("pallet_num", putGoods.getPalletNum());
    map.put("order_num", putGoods.getOrderNum()==null?null:putGoods.getOrderNum()+"");
		map.put("update_user", loginUser.getUserId() + "");
		map.put("id", putGoods.getId());
		getMapParameter(sql, map);
		int count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean updateNum(PutGoods putGoods, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("update chf_put_goods set ");
		sql.append("num=:num,weight=:weight ");
		sql.append(",total_weight=:total_weight ");
		sql.append(",volume=:volume,total_volume=:total_volume ");
		sql.append(",update_time=now() ");
		sql.append(",update_user=:update_user where id=:id ");
    map.put("num", putGoods.getNum()+"");
    map.put("weight", putGoods.getWeight()+"");
    map.put("total_weight", putGoods.getTotalWeight()+"");
    map.put("volume", putGoods.getVolume()+"");
    map.put("total_volume", putGoods.getTotalVolume()+"");
		map.put("update_user", loginUser.getUserId() + "");
		map.put("id", putGoods.getId());
		getMapParameter(sql, map);
		int count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean remove(PutGoods putGoods, User loginUser) {
		int count = 0;
		Map<String, String> map = null;
		if(putGoods.getId().length()>0){
			for(String ID : putGoods.getId().split(",")){
				map  = new HashMap<String, String>();
				if(ID==null || ID.equals("")){ break; }
				StringBuffer sql =new StringBuffer("update chf_put_goods set deleted=1" +
						",update_user=:update_user, update_time=now() "
						+ " where id =:id ");
				map.put("update_user", loginUser.getUserId() + "");
				map.put("id", ID);
				getMapParameter(sql, map);
				count = jdbcTemplate.update(sql.toString(),map);
			}
		}
		return count > 0;
	}
}
